package com.zsw.dubbo.filter;

import com.alibaba.dubbo.rpc.Filter;
import com.alibaba.dubbo.rpc.Invocation;
import com.alibaba.dubbo.rpc.Invoker;
import com.alibaba.dubbo.rpc.Result;
import com.alibaba.dubbo.rpc.RpcException;
import com.alibaba.dubbo.rpc.RpcResult;

public class ProviderFilter implements Filter  {

	private String username = "admin";
	private String password = "admin0";
	
	@Override
	public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
		 // before filter ...
		String username_ = invocation.getAttachment("username");
		String password_ = invocation.getAttachment("password");
		if(username_ == null || password_ == null){
			return new RpcResult(new IllegalArgumentException("未找到用户！"));
		}
		if(!username.equals(username_) || !password.equals(password_)){
			return new RpcResult(new IllegalArgumentException("用户名或密码错误"));
		}
		Result result = invoker.invoke(invocation);
		return result;
	}
}
